package com.tddx.activiti.base;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Enumeration;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.tddx.activiti.common.IdGenerator;
import com.tddx.activiti.utils.StringUtils;

public class BaseJdbc<T extends BaseEntity> {
	
	@Autowired protected HttpServletRequest request;
	@PersistenceContext protected EntityManager em;
	
	protected Logger log = Logger.getLogger(this.getClass());
	
	Class<T> entityClass;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public BaseJdbc(){
		Type genType = getClass().getGenericSuperclass();   
		Type[] params = ((ParameterizedType) genType).getActualTypeArguments();   
		entityClass =  (Class)params[0];
	}
	
	@SuppressWarnings("unchecked")
	public List<T> findListByCondition(){
		Query query = em.createQuery(getSql() + this.getOrderBy());
		return query.getResultList();
	}
	
	public String getSql(){
		StringBuffer sb = new StringBuffer(" FROM " + getSimpleName() + " WHERE vcServer= '" + IdGenerator.serverId + "'");
		Enumeration<?> enums = request.getParameterNames();
		while(enums.hasMoreElements()){
			String key = (String)enums.nextElement();
			String value = request.getParameter(key);
			if(StringUtils.isNotEmpty(value)){
				if(key.startsWith("f_like_")){
					String simplekey = key.substring(7);
					sb.append(" AND " + simplekey + " LIKE '%" + value + "%'");
				}else if(key.startsWith("f_eq_")){
					String simplekey = key.substring(5);
					sb.append(" AND " + simplekey + " = '" + value + "'");
				}else if(key.startsWith("f_le_")){
					String simplekey = key.substring(8);
					sb.append(" AND " + simplekey + " >= '" + value + "'");
				}else if(key.startsWith("f_gt_")){
					String simplekey = key.substring(6);
					sb.append(" AND " + simplekey + " <= '" + value + "'");
				}else if(key.startsWith("f_begin_")){
					String simplekey = key.substring(8);
					sb.append(" AND " + simplekey + " >= TO_DATE('" + value + "', 'yyyy-MM-dd')");
				}else if(key.startsWith("f_end_")){
					String simplekey = key.substring(6);
					sb.append(" AND " + simplekey + " <= TO_DATE('" + value + "', 'yyyy-MM-dd')");
				}
			}
		}
		return sb.toString();
	}
	
	public String getOrderBy(){
		StringBuffer sb = new StringBuffer("");
		Enumeration<?> enums = request.getParameterNames();
		while(enums.hasMoreElements()){
			String key = (String)enums.nextElement();
			String value = request.getParameter(key);
			if(StringUtils.isNotEmpty(value)){
				if(key.startsWith("f_asc_")){
					String simplekey = key.substring(7);
					sb.append(" ORDER BY " + simplekey);
				}else if(key.startsWith("f_desc_")){
					String simplekey = key.substring(5);
					sb.append(", " + simplekey + " DESC");
				}
			}
		}
		return sb.toString();
	}
	
	public String getSimpleName(){
		try {
			return entityClass.getSimpleName();
		} catch (Exception e) {
			log.error("获取泛型类名报错:" + e);
			e.printStackTrace();
		}
		return "";
	}
	
	public String getName(){
		try {
			return entityClass.getName();
		} catch (Exception e) {
			log.error("获取泛型全类名报错" + e);
			e.printStackTrace();
		}
		return "";
	}
	
}
